﻿using FirstFloor.ModernUI.Presentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;

namespace EasyInvoice.WPF.ViewModel
{
    public class IntroductionViewModel:NotifyPropertyChanged
    {
        private string _user = string.Empty;
        private int _missingMonths = 0;
        private int _invoicingDay = Properties.Settings.Default.InvoicingDate;
        private Services.InvoicesService _serviceInvoices;

        public string User
        {
            get
            {
                if (EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser != null)
                    return ((UserViewModel)EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser).UserName;
                return string.Empty;
            }

        }

        public int MissingMonths
        {
            get
            {
                var invoices = this._serviceInvoices.GetAll();
                if (invoices.Count() > 0)
                {
                    DateTime lastInvoice = invoices.Max(m => m.Date);
                    DateTime currentInoiceDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, _invoicingDay);
                    while (lastInvoice < currentInoiceDate)
                    {
                        lastInvoice = lastInvoice.AddMonths(1);
                        _missingMonths++;
                    }
                }
                //TimeSpan timeSpan = DateTime.Today.Subtract(lastInvoice);
                
                return _missingMonths;
            }
            set
            {
                _missingMonths = value;
                OnPropertyChanged("MissingMonths");
            }
        }

        public int InvoicingDay
        {
            get
            {
                return _invoicingDay;
            }
            set
            {
                _invoicingDay = value;
                OnPropertyChanged("InvoicingDay");
            }
        }

        public IntroductionViewModel() { this._serviceInvoices = appCtx.Instance.IoC.Resolve<Services.InvoicesService>(); }
    }
}
